home *** CD-ROM | disk | FTP | other *** search
- Path: chronicle.mti.sgi.com!austern
- From: clamage@Eng.Sun.COM (Steve Clamage)
- Newsgroups: comp.std.c++
- Subject: Re: Bit-field specification incomplete?
- Date: 19 Mar 1996 12:07:17 PST
- Organization: Sun Microsystems Inc.
- Approved: austern@isolde.mti.sgi.com
- Message-ID: <4in39p$krf@engnews1.Eng.Sun.COM>
- References: <sdouglas-1903961604090001@193.131.176.202>
- Reply-To: clamage@Eng.Sun.COM
- NNTP-Posting-Host: isolde.mti.sgi.com
- X-Original-Date: 19 Mar 1996 19:52:25 GMT
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBVAwUBMU8UIEy4NqrwXLNJAQE8igH/d0UYakGx4tyEh8z6WAgyrC1lF4PJpwOc
- D/UvCYwU976zQ+FXZSlT39dYKxxLWNY/Ay+oj/ILN9utXIQXhqXmYA==
- =vulz
- Originator: austern@isolde.mti.sgi.com
-
- In article 1903961604090001@193.131.176.202, sdouglas@armltd.co.uk (scott douglass) writes:
- >
- >I was surprised to find that section 9.6, Bit-fields, does not prohibit
- >'struct T { int i:-1; };' or 'struct T { int i:0; };' which are constraint
- >violations in the C standard.
-
- That looks to me like an oversight in the draft.
-
- > It also does not explicitly state that the
- >member must act as an integer of the specified numbers of bits.
-
- I believe that is deliberate. The actual amount of space allocated is
- implementation-dependent, as long as it is at least the amount specified.
- If you specify a "long" member bitfield of 8 bits, the actual behavior will
- be implementation-dependent. If you specify a bitfield wider than the
- type, there is no portable way to determine what happens to the extra bits.
- Example: Suppose type short is 16 bits, and you write
- short data : 24;
- You can expect to store any short value in the bitfield and get that
- value back when you read the bitfield. Anything beyond that, including
- what happens if you increment "data" beyond SHORT_MAX, depends on
- the implementation. (Some of these points were discussed at the C++ Committee
- meeting last week, and it was decided not to specify the behavior. The
- reason is that so much depends on the implementation already that there
- isn't much the standard could usefully say.)
-
- >Since the draft says nothing specifically about the type of a bit-field
- >member, I assume that it is the type of the member ignoring the bit-field
- >specifier.
-
- That is correct. The bitfield specification is a request to the compiler to
- perform some implementation-dependent packing of the data. The type of the
- member is the declared type. (Use a too-short bitfield at your own risk.)
-
- >Finally, the promotion of a bit-field is specified in 4.5, Integral
- >promotions. It specifies that all non-enum bit-fields promote to 'int' or
- >'unsigned int'. This seems wrong for 'long' and 'unsigned long'
- >bit-fields.
-
- That has already been fixed. The current draft says promotion occurs only
- if type int (or unsigned int) can represent all the values of the bitfield.
- If the bitfield is larger, no promotion occurs.
-
- ---
- Steve Clamage, stephen.clamage@eng.sun.com
- ---
- [ comp.std.c++ is moderated. To submit articles: Try just posting with your
- newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
- comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
- Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-